package com.sinosoft.cloud.util;

import com.sinosoft.utility.ExceptionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class GetBizNo {
    final static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    final static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
    final static SimpleDateFormat dateAndTimeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    static Log log = LogFactory.getLog(GetBizNo.class);
    public static String getReqMsgId() {
        StringBuffer res = new StringBuffer("");
        res.append(dateAndTimeFormat.format(new Date()));
        res.append(String.valueOf(System.currentTimeMillis()));
        res.append(getRandomStr(6));
        return res.toString();
    }

    public static String getOutBizNo() {
        StringBuffer res = new StringBuffer("");
        res.append(dateAndTimeFormat.format(new Date()));
        res.append(getRandomStr(9));
        return res.toString();
    }

    public static String getInstSerialNo() {
        StringBuffer res = new StringBuffer("");
        res.append(dateFormat.format(new Date()));
        res.append("ZAOL");
        res.append(getRandomStr(18));
        return res.toString();
    }

    public static String getPolicyNo() {
        StringBuffer res = new StringBuffer("18");
        res.append(getRandomStr(18));
        return res.toString();
    }

    public static String getDate(String dateStr) {
        Date date = null;
        try {
            date = dateAndTimeFormat.parse(dateStr);
        } catch (ParseException e) {
            log.error("日期解析异常"+ ExceptionUtils.exceptionToString(e));
            e.printStackTrace();
            return "";
        }
        return dateFormat.format(date);
    }
    public static String getFullDate(String dateStr) {
        Date date = null;
        try {
            date = dateAndTimeFormat.parse(dateStr);
        } catch (ParseException e) {
            log.error("日期解析异常"+ ExceptionUtils.exceptionToString(e));
            e.printStackTrace();
            return "";
        }
        return simpleDateFormat.format(date);
    }


    public static String getRandomStr(int max) {
        int serio = 1;
        while (--max > 0) {
            serio *= 10;
        }
        long res = (long) ((Math.random() + 1) * serio);
        return String.valueOf(res);
    }

}
